package com.hzvdvmc.autosale.setting.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.hzvdvmc.autosale.MyApp;
import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.constant.SpKey;
import com.hzvdvmc.autosale.setting.adapter.SimpleTextSpinnerAdatper;
import com.hzvdvmc.autosale.util.SPUtils;

import java.util.ArrayList;

import android_serialport_api.SeriaPortClient;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SerialAddressActivity extends Activity {

    @BindView(R.id.spinner_a)
    Spinner spinnerA;
    @BindView(R.id.spinner_b)
    Spinner spinnerB;
    @BindView(R.id.spinner_c)
    Spinner spinnerC;
    @BindView(R.id.spinner_d)
    Spinner spinnerD;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_func)
    TextView tvFunc;

    private SpinnerAdapter mAdapter;
    private ArrayList<String> dataList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serial_address);
        ButterKnife.bind(this);
        tvTitle.setText("串口地址设置");
        tvFunc.setVisibility(View.VISIBLE);
        tvFunc.setText("保存");
        initData();
    }

    private void initData() {
        dataList.clear();
        dataList.add("无");
        dataList.addAll(MyApp.seriList);
        mAdapter = new SimpleTextSpinnerAdatper(dataList);
        spinnerA.setAdapter(mAdapter);
        spinnerA.setPrompt("驱动A地址设置");

        spinnerB.setAdapter(mAdapter);
        spinnerB.setPrompt("驱动B地址设置");

        spinnerC.setAdapter(mAdapter);
        spinnerC.setPrompt("驱动C地址设置");

        spinnerD.setAdapter(mAdapter);
        spinnerD.setPrompt("驱动D地址设置");

        String devA = SPUtils.getInstance().getString(SpKey.SERIAL_A, SeriaPortClient.DEV_A);
        String devB = SPUtils.getInstance().getString(SpKey.SERIAL_B, SeriaPortClient.DEV_B);
        String devC = SPUtils.getInstance().getString(SpKey.SERIAL_C, SeriaPortClient.DEV_B);
        String devD = SPUtils.getInstance().getString(SpKey.SERIAL_D, SeriaPortClient.DEV_B);

        spinnerA.setSelection(dataList.indexOf(devA));
        spinnerB.setSelection(dataList.indexOf(devB));
        spinnerC.setSelection(dataList.indexOf(devC));
        spinnerD.setSelection(dataList.indexOf(devD));

    }

    @OnClick({R.id.iv_back, R.id.tv_func})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_func:
                saveData();
                finish();
                break;
        }
    }

    private void saveData() {
        String serialA = dataList.get(spinnerA.getSelectedItemPosition());
        String serialB = dataList.get(spinnerB.getSelectedItemPosition());
        String serialC = dataList.get(spinnerC.getSelectedItemPosition());
        String serialD = dataList.get(spinnerD.getSelectedItemPosition());
        SPUtils.getInstance().put(SpKey.SERIAL_A, serialA);
        SPUtils.getInstance().put(SpKey.SERIAL_B, serialB);
        SPUtils.getInstance().put(SpKey.SERIAL_C, serialC);
        SPUtils.getInstance().put(SpKey.SERIAL_D, serialD);
    }

}
